<template>
  <div class="admin-delivery-management">
    <el-card>
      <template #header>
        <div class="card-header">
          <h2>配送管理</h2>
          <el-button type="primary" @click="handleAddDelivery">新建配送任务</el-button>
        </div>
      </template>

      <!-- 配送任务列表 -->
      <el-table :data="deliveries" style="width: 100%">
        <el-table-column prop="delivery_number" label="配送编号" width="180" />
        <el-table-column prop="order_id" label="关联订单" width="120" />
        <el-table-column prop="delivery_staff" label="配送员" width="120" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" width="180" />
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleEdit(row)">编辑</el-button>
            <el-button type="success" link @click="handleViewDetails(row)">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '新建配送任务' : '编辑配送任务'"
      width="50%"
    >
      <el-form :model="deliveryForm" label-width="100px">
        <el-form-item label="配送员">
          <el-select v-model="deliveryForm.delivery_staff" placeholder="请选择配送员">
            <el-option
              v-for="staff in availableStaff"
              :key="staff.id"
              :label="staff.name"
              :value="staff.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="deliveryForm.status" placeholder="请选择状态">
            <el-option label="待处理" value="pending" />
            <el-option label="配送中" value="delivering" />
            <el-option label="已完成" value="completed" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="deliveryForm.note" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'

// 配送任务列表数据
const deliveries = ref([
  {
    id: 1,
    delivery_number: 'DEL20240301001',
    order_id: 1,
    delivery_staff: '配送员A',
    status: 'pending',
    created_at: '2024-03-01 09:30:00'
  },
  {
    id: 2,
    delivery_number: 'DEL20240301002',
    order_id: 2,
    delivery_staff: '配送员B',
    status: 'delivering',
    created_at: '2024-03-01 10:00:00'
  },
  {
    id: 3,
    delivery_number: 'DEL20240301003',
    order_id: 3,
    delivery_staff: '配送员C',
    status: 'completed',
    created_at: '2024-03-01 11:30:00'
  }
])

// 可用配送员列表
const availableStaff = ref([
  { id: 1, name: '配送员A', status: 'available' },
  { id: 2, name: '配送员B', status: 'available' },
  { id: 3, name: '配送员C', status: 'busy' }
])

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(3)

// 对话框相关
const dialogVisible = ref(false)
const dialogType = ref('add')
const deliveryForm = ref({
  delivery_staff: '',
  status: 'pending',
  note: ''
})

// 获取状态样式
const getStatusType = (status) => {
  const typeMap = {
    pending: 'warning',
    delivering: 'primary',
    completed: 'success'
  }
  return typeMap[status] || 'info'
}

// 获取状态文本
const getStatusText = (status) => {
  const textMap = {
    pending: '待处理',
    delivering: '配送中',
    completed: '已完成'
  }
  return textMap[status] || '未知'
}

// 处理新建配送任务
const handleAddDelivery = () => {
  dialogType.value = 'add'
  deliveryForm.value = {
    delivery_staff: '',
    status: 'pending',
    note: ''
  }
  dialogVisible.value = true
}

// 处理编辑
const handleEdit = (row) => {
  dialogType.value = 'edit'
  deliveryForm.value = { ...row }
  dialogVisible.value = true
}

// 处理查看详情
const handleViewDetails = (row) => {
  console.log('查看配送任务详情:', row)
}

// 处理提交
const handleSubmit = () => {
  console.log('提交配送任务:', deliveryForm.value)
  ElMessage.success(dialogType.value === 'add' ? '新建成功' : '更新成功')
  dialogVisible.value = false
}

// 处理分页大小变化
const handleSizeChange = (val) => {
  pageSize.value = val
  // 重新加载数据
}

// 处理页码变化
const handleCurrentChange = (val) => {
  currentPage.value = val
  // 重新加载数据
}

onMounted(() => {
  // 这里可以加载实际数据
  console.log('配送管理页面加载完成')
})
</script>

<style scoped>
.admin-delivery-management {
  padding: 24px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 